package com.advert.boot.common.util;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileMD5Utils {


    /**
     * multipartfile .size 转kb
     *
     * @param fileSizeInBytes 获取文件大小（字节）
     * @return 转换为KB，同时保留两位小数
     */
    public static double convertToKB(long fileSizeInBytes) {
        //转换为KB:由于1KB等于1024字节，直接将文件大小除以1024来获取KB值
        BigDecimal sizeInKB = new BigDecimal(fileSizeInBytes).divide(new BigDecimal(1024), 2, RoundingMode.HALF_UP);

        return sizeInKB.doubleValue();
    }

    /**
     * 使用Java标准库的MessageDigest类获取MD5值
     *
     * @param filePath 文件路径
     * @return 文件的md5值
     */
    public static String getMD5ByMessageDigest(String filePath) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            try (InputStream is = new FileInputStream(filePath)) {
                byte[] buffer = new byte[8192];
                int read;
                while ((read = is.read(buffer)) > 0) {
                    md.update(buffer, 0, read);
                }
            }
            byte[] md5 = md.digest();

            StringBuilder result = new StringBuilder();
            for (byte b : md5) {
                result.append(String.format("%02x", b));
            }

            return result.toString();
        } catch (NoSuchAlgorithmException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用Apache Commons Codec库获取MD5值
     *
     * @param filePath 文件路径
     * @return 文件的md5值
     */
    public static String getMD5ByApacheCommonsCodec(String filePath) {
        try {
            return DigestUtils.md5Hex(new FileInputStream(filePath));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用Java NIO获取MD5值
     *
     * @param filePath 文件路径
     * @return 文件的md5值
     */
    public static String getMD5ByNIO(String filePath) {
        try {
            Path path = new File(filePath).toPath();
            try (InputStream is = java.nio.file.Files.newInputStream(path, StandardOpenOption.READ)) {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] buffer = new byte[8192];
                int read;
                while ((read = is.read(buffer)) > 0) {
                    md.update(buffer, 0, read);
                }
                byte[] md5 = md.digest();

                StringBuilder result = new StringBuilder();
                for (byte b : md5) {
                    result.append(String.format("%02x", b));
                }

                return result.toString();
            }
        } catch (NoSuchAlgorithmException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) {
        String filePath = "E:\\客服账分全.xlsx";

        String md5ByMessageDigest = getMD5ByMessageDigest(filePath);
        System.out.println("MD5 (java MessageDigest 标准库实现): " + md5ByMessageDigest);

        String md5ByApacheCommonsCodec = getMD5ByApacheCommonsCodec(filePath);
        System.out.println("MD5 (Apache Commons Codec 库实现): " + md5ByApacheCommonsCodec);

        String md5ByNIO = getMD5ByNIO(filePath);
        System.out.println("MD5 (Java NIO 实现): " + md5ByNIO);


        String a = "abc";
        String b = "abc";
        String c = new String("abc");
        String d = "ab" + "c";

        System.out.println( a == b);
        System.out.println( a == c);
        System.out.println( a == d);
        System.out.println( c == d);

        System.out.println( Math.round(-1.6));
        System.out.println( Math.round(-1.5));
        System.out.println( Math.round(-1.9));
        System.out.println( Math.round(-1.1));
        System.out.println( Math.round(-2.6));


        System.out.println( Math.round(1.6));
        System.out.println( Math.round(1.5));
        System.out.println( Math.round(1.9));
        System.out.println( Math.round(1.1));
        System.out.println( Math.round(2.6));
    }
}
